package com.itheima.service.mongo.Impl;

import com.itheima.domain.mongo.Comment;
import com.itheima.domain.mongo.Movement;
import com.itheima.domain.vo.PageBeanVo;
import com.itheima.service.mongo.CommentService;
import org.apache.dubbo.config.annotation.DubboService;
import org.bson.types.ObjectId;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.data.domain.Sort;
import org.springframework.data.mongodb.core.MongoTemplate;
import org.springframework.data.mongodb.core.query.Criteria;
import org.springframework.data.mongodb.core.query.Query;

import java.util.List;

@DubboService
public class CommentServiceImpl implements CommentService {
    @Autowired
    private MongoTemplate mongoTemplate;

    @Override
    public Integer saveComment(Comment comment) {
        System.out.println("进入saveComment");
        mongoTemplate.save(comment);
        //获得动态的类型
        //先找到原动态
        Movement movement = mongoTemplate.findById(comment.getPublishId(), Movement.class);
        switch (comment.getCommentType()) {
            case 1: {//setLikeCount
                movement.setLikeCount(movement.getLoveCount() + 1);
                mongoTemplate.save(movement);
                //需要返回现在的数量
                return movement.getLoveCount();
            }
            case 2: {//setCommentCount
                movement.setCommentCount(movement.getCommentCount() + 1);
                mongoTemplate.save(movement);
                //需要返回现在的数量
                return movement.getCommentCount();
            }
            case 3: {//setLoveCount
                movement.setLoveCount(movement.getLikeCount() + 1);
                mongoTemplate.save(movement);
                //需要返回现在的数量
                return movement.getLikeCount();
            }
            case 4: {
            }
            case 5: {
            }
            case 6: {
            }
        }
        return 0;
    }

    @Override
    public Integer removeComment(Long userId, ObjectId publishId, Integer commentType) {
        Query query = new Query(Criteria.where("userId").is(userId)
                .and("publishId").is(publishId)
                .and("commentType").is(commentType));
        mongoTemplate.remove(query, Comment.class);
        //找到当前动态对状态数量进行修改
        Movement movement = mongoTemplate.findById(publishId, Movement.class);
        switch (commentType) {
            case 1: {//setLikeCount
                movement.setLikeCount(movement.getLoveCount() - 1);
                mongoTemplate.save(movement);
                //需要返回现在的数量
                return movement.getLoveCount();
            }
            case 2: {//setCommentCount
                movement.setCommentCount(movement.getCommentCount() - 1);
                mongoTemplate.save(movement);
                //需要返回现在的数量
                return movement.getCommentCount();
            }
            case 3: {//setLoveCount
                movement.setLoveCount(movement.getLikeCount() - 1);
                mongoTemplate.save(movement);
                //需要返回现在的数量
                return movement.getLikeCount();
            }
            case 4: {
            }
            case 5: {
            }
            case 6: {
            }
        }

        return 0;
    }

    @Override
    public PageBeanVo findCommentByPage(ObjectId publishId, Integer commentType, Integer pageNum, Integer pageSize) {
        int starIndex = (pageNum - 1) * pageSize;
        Query query = new Query(
                Criteria.where("publishId").is(publishId)
                        .and("commentType").is(commentType))
                .with(Sort.by(Sort.Order.desc("created")))
                .skip(starIndex).limit(pageSize);
        List<Comment> comments = mongoTemplate.find(query, Comment.class);
        //查询评论总数不要分页
        Query queryCount = new Query(Criteria.where("publishId").is(publishId)
                .and("commentType").is(commentType)
        );
        long count = mongoTemplate.count(queryCount, Comment.class);
        return new PageBeanVo(pageNum, pageSize, count, comments);
    }

    @Override
    public PageBeanVo findMyCommentByPage(Long publishUserId, Integer commentType, Integer pageNum, Integer pageSize) {
        int starIndex = (pageNum - 1) * pageSize;

        Query query = new Query(Criteria.where("commentType").is(commentType)
                .and("publishUserId").is(publishUserId)).with(Sort.by(Sort.Order.desc("created"))).skip(starIndex).limit(pageSize);
        List<Comment> commentList = mongoTemplate.find(query, Comment.class);
        long count = mongoTemplate.count(query, Comment.class);
        return new PageBeanVo(pageNum, pageSize, count, commentList);
    }
}
